import Taro ,{ Component } from '@tarojs/taro'
import { View } from '@tarojs/components'

import {AtTabs, AtTabsPane} from 'taro-ui'

import './myExchange.scss'

import ListEmptyView from '../../component/LiseEmptyView/listEmptyView.js'
import emptyIcon from '../../image/ic_empty.png'
import ExchangeItem from '../../component/ExchangeItem/exchangeItem.js'
import {COMMON_CONST} from "../../utils/const";
import {base_url} from "../../network/config";
import DataManager from "../../utils/DataManager";


let notReceivePage = 1;
let receivedPage = 1;
let overduePage = 1;

export default class myExchange extends Component{

  config = {
    navigationBarTitleText: '我的兑换'
  }

  constructor() {
    super(...arguments)
    this.state = {
      notReceiveList:[],
      receivedList:[],
      overdueList:[],
      tabCurrent: 0,
    }
  }


  onPullDownRefresh(){
    notReceivePage = 1;
    receivedPage = 1;
    overduePage = 1;
    this.loadMyExchanges(notReceivePage , 0)
    this.loadMyExchanges(receivedPage , 1)
    this.loadMyExchanges(overduePage , 2)
  }

  onReachBottom(){
    if (this.state.tabCurrent == 0){
      notReceivePage ++;
      this.loadMyExchanges(notReceivePage , 0)
    } else if (this.state.tabCurrent == 1){
      receivedPage ++;
      this.loadMyExchanges(receivedPage , 1)
    } else if (this.state.tabCurrent == 2){
      overduePage ++;
      this.loadMyExchanges(overduePage , 1)
    }
  }

  componentDidMount(){
    this.loadMyExchanges(notReceivePage , 0);
    this.loadMyExchanges(receivedPage , 1);
    this.loadMyExchanges(overduePage , 2);

    Taro.eventCenter.on('onGoodReceive',() => {
      notReceivePage = 1;
      receivedPage = 1;
      this.loadMyExchanges(notReceivePage , 0);
      this.loadMyExchanges(receivedPage , 1);
    })
  }

  //type------0为待领取，1为已领取，2为已过期
  loadMyExchanges(page , type){
    console.log('loadpage------------' + page)

    let _this = this;
    let url = base_url + 'rest/user/receive/goodsList'
    Taro.request({
      url:url,
      data: {
        token:DataManager.getToken(),
        platform:'android_c',
        weixinId:DataManager.getWeiXinId(),
        loginChannel:'c',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
        pageNo:page,
        pageSize:COMMON_CONST.LIST_SIZE,
        receiveStatus:type
      },
      method: 'GET',
      success(res) {

        if (!res.data || !res.data.data || !res.data.data.list || res.data.data.list.length < 1){
          if (type == 0){
            if (notReceivePage > 1){
              notReceivePage --;
            }
          } else if (type == 1){
            if (receivedPage > 1){
              receivedPage --;
            }
          } else if (type == 2){
            if (overduePage > 1){
              overduePage --;
            }
          }
        }

        let subList = [];

        if (res && res.data && res.data.data && res.data.data.list){
          if (type == 0){
            if (page == 1){
              subList = res.data.data.list;
            } else {
              subList = _this.state.notReceiveList.concat(res.data.data.list)
            }
            _this.setState({
              notReceiveList:subList
            })
          } else if (type == 1){
            if (page == 1){
              subList = res.data.data.list;
            } else {
              subList = _this.state.receivedList.concat(res.data.data.list)
            }
            _this.setState({
              receivedList:subList
            })
          } else if (type == 2){
            if (page == 1){
              subList = res.data.data.list;
            } else {
              subList = _this.state.overdueList.concat(res.data.data.list)
            }
            _this.setState({
              overdueList:subList
            })
          }

        }
      },
      fail(){
        if (type == 0){
          if (notReceivePage > 1){
            notReceivePage --;
          }
        } else if (type == 1){
          if (receivedPage > 1){
            receivedPage --;
          }
        } else if (type == 2){
          if (overduePage > 1){
            overduePage --;
          }
        }
      },
      complete(){
        Taro.stopPullDownRefresh();
      }
    })
  }



  handleTabClick = (pos) =>{
    this.setState({
      tabCurrent: pos
    })
  }

  render(){
    let tabList = [{title: '未领取'} , {title: '已领取'} , {title: '已过期'}]

    return(
      <View className='my-exchange-container'>
        <AtTabs current={this.state.tabCurrent} tabList={tabList} onClick={this.handleTabClick.bind(this)}  swipeable={false}>
          <AtTabsPane current={this.state.tabCurrent} index={0}>
            {this.state.notReceiveList.length > 0 ? this.state.notReceiveList.map(item => {
                return <ExchangeItem scoreData={item} goodsTotalCount={item.totalCount}></ExchangeItem>
              })
              :
              <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
            }
          </AtTabsPane>
          <AtTabsPane current={this.state.tabCurrent} index={1}>
            {this.state.receivedList.length > 0 ? this.state.receivedList.map(item => {
                return <ExchangeItem scoreData={item} goodsTotalCount={item.totalCount}></ExchangeItem>
              })
              :
              <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
            }
          </AtTabsPane>
          <AtTabsPane current={this.state.tabCurrent} index={2} >
            {this.state.overdueList.length > 0 ? this.state.overdueList.map(item => {
                return <ExchangeItem scoreData={item} goodsTotalCount={item.totalCount}></ExchangeItem>
              })
              :
              <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
            }
          </AtTabsPane>
        </AtTabs>
      </View>
    )
  }

}

